package com.mengwen.backstage.util;

import com.mengwen.backstage.pojo.Chart;
import com.mengwen.backstage.pojo.RankKeyName;
import com.mengwen.backstage.pojo.WeixinUser;
import com.mengwen.backstage.service.PcUserService;
import com.mengwen.backstage.service.RankService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author LiangYonghui
 * @date 2020/4/26 14:54
 * @description GetCharsUtil(获取管理员后台的实时数据)
 */
@Component
@Slf4j
public class GetCharsUtil {
    @Autowired
    GetUserInfoUtil getUserInfoUtil;

    @Autowired
    PcUserService pcUserService;

    @Autowired
    RankService rankService;

    public List<Chart> getCharts(String keyName) throws Exception {
        // 从缓存中获取money和unionid
        Set<ZSetOperations.TypedTuple<String>> typedTuples = rankService.listRank(keyName, 0, 10);

        Iterator<ZSetOperations.TypedTuple<String>> iterator = typedTuples.iterator();

        List<Chart> charts = new ArrayList<>();

        List<String> openids = new LinkedList<>();
        // 防止空指针 初始化十个：
        for (int j = 0; j < 10; j++) {
            openids.add("");
        }

        // 根据unionid获取openid
        List<WeixinUser> list = null;
        // 记录money
        List<Double> moneys = new LinkedList<>();
        int i = 1;
        while (iterator.hasNext()) {
            ZSetOperations.TypedTuple<String> next = iterator.next();
            String unionid = next.getValue(); // unionid
            Double money = next.getScore(); // money
            moneys.add(money);
            log.info("一一对应关系2：");
            log.info(unionid + ":" + money);
            // 根据unionid查询openid
            String openid = pcUserService.findOpenidByUnionid(unionid);
            log.info("获取到的openid:");
            log.info(openid);
            if ("".equals(openid) || null == openid) {
                log.info("unionid :" + unionid + "对应的openid为空");
            } else {
                // 将前十或十以内的openid存起来
                System.out.println("这里的i:" + i);
                openids.add(i - 1, openid);
                i++;
            }

        }

        log.info("---------------openids----------------");

        // 还得记录一下money
        log.info("" + openids);

        // 将列表中的openid转化为字符串数组
        String[] strs1 =openids.toArray(new String[openids.size()]);


        // 获取openid字符串数组对应的用户信息
        list = getUserInfoUtil.getTenValueByKeyAndOpenid(strs1);

        ;
        for (int z = 0; z < i - 1; z++) {
            String imgurl = list.get(z).getHeadimgurl();
            String nickname = list.get(z).getNickname();
            charts.add(new Chart(nickname,imgurl,moneys.get(z),z + 1));
        }


        log.info("------------------------------------------");
        log.info( "" +charts);
        log.info("-------------------------------------");

        return charts;

    }
}
